package com.liliudong.cursormate.service.impl;

import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liliudong.cursormate.common.constant.ConfigConst;
import com.liliudong.cursormate.common.constant.SecurityConst;
import com.liliudong.cursormate.common.exception.BusinessException;
import com.liliudong.cursormate.entity.SysUser;
import com.liliudong.cursormate.mapper.SysPermissionMapper;
import com.liliudong.cursormate.mapper.SysRoleMapper;
import com.liliudong.cursormate.mapper.SysUserMapper;
import com.liliudong.cursormate.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author Liudong.li
 * @description 针对表【sys_user(系统用户表)】的数据库操作Service实现
 * @createDate 2023-10-24 22:29:24
 */
@Service
@ConditionalOnProperty(name = {ConfigConst.CURSOR_MATE_POLICY}, havingValue = ConfigConst.CURSOR_MATE_POLICY_RULE)
@RequiredArgsConstructor
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser>
        implements SysUserService {

    protected final SysRoleMapper sysRoleMapper;
    protected final SysPermissionMapper sysPermissionMapper;

    @Override
    public SaTokenInfo login(SysUser sysUser) {
        SysUser userByUsername = baseMapper.selectOne(
                Wrappers.lambdaQuery(SysUser.class)
                        .select(SysUser::getId, SysUser::getPassword, SysUser::getOrgNo, SysUser::getDataScope)
                        .eq(SysUser::getUsername, sysUser.getUsername())
        );
        if (Objects.isNull(userByUsername)) {
            throw new BusinessException("用户不存在");
        }
        boolean bcryptCheck = DigestUtil.bcryptCheck(sysUser.getPassword(), userByUsername.getPassword());
        if (!bcryptCheck) {
            throw new BusinessException("密码不正确");
        }
        StpUtil.login(
                userByUsername.getId(),
                SaLoginConfig
                        .setExtra(SecurityConst.ORG_NO, userByUsername.getOrgNo())
                        .setExtra(SecurityConst.DATA_SCOPE, userByUsername.getDataScope())
        );
        return StpUtil.getTokenInfo();
    }

    @Override
    public SysUser profile() {
        long userId = StpUtil.getLoginIdAsLong();
        SysUser sysUser = baseMapper.selectById(userId);
        List<String> roles = sysRoleMapper.selectRoleListByUserIdInterceptorIgnore(userId);
        List<String> permissions = sysPermissionMapper.selectPermissionListByUserIdInterceptorIgnore(userId);
        sysUser.setRoleCodeList(roles);
        sysUser.setPermissionList(permissions);
        return sysUser;
    }

    @Override
    public int countByOrgId(Long orgId) {
        return baseMapper.selectCountByOrgId(orgId);
    }
}




